﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FinalProject.DAL.Mapping;
using FinalProject.DAL.Model;

namespace FinalProject.BL
{
    public class CustomerBL : BaseBL<Customer>
    {
        public static bool IsExistedCode(string code)
        {
            int count = dalContext.Customers.Where(p => p.Code == code).Count();
            return count > 0;
            //using (InvoiceModelContext dalContext = new InvoiceModelContext(Utilities.ConnectionString))
            //{

            //}
        }

        public static bool InsertOrUpdate(Customer customer)
        {
            try
            {
                if (IsExistedCode(customer.Code))
                {

                    //dalContext.Customers.Attach(customer);
                    //var osm = dalContext.ObjectStateManager;
                    //osm.ChangeObjectState(customer, System.Data.EntityState.Modified);
                }
                else
                    dalContext.Customers.AddObject(customer);
                dalContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Customer GetCodeStartWith(string code)
        {
            try
            {
                var query = dalContext.Customers.Where(p => p.Code.StartsWith(code)).FirstOrDefault();
                return query;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static bool CanDebit(Guid customerId, decimal currentDebit)
        {
            try
            {
                decimal maxDebit = dalContext.Customers
                                            .Where(p => p.Id == customerId)
                                            .Select(p => p.MaxDebit)
                                            .SingleOrDefault();

                decimal custBalance = dalContext.CustomerBalances
                                                .Where(p => p.CustomerId == customerId)
                                                .Select(p => (p.TotalDebit - p.TotalCreadit))
                                                .SingleOrDefault() ?? decimal.Zero;
                decimal totalDebit = decimal.Zero;
                if (custBalance < 0)
                {
                    totalDebit = Math.Abs(custBalance) + currentDebit;
                    return totalDebit <= maxDebit;
                }
                else if (custBalance > 0)
                {
                    totalDebit = custBalance - currentDebit;
                    return Math.Abs(custBalance) <= maxDebit;
                }
                else
                    return currentDebit <= maxDebit;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
